package com.dingreading.cloud.soms.service.impl;

import com.dingreading.cloud.soms.entity.Cmpaypics;
import com.dingreading.cloud.soms.entity.table.CmpaypicsTableDef;
import com.dingreading.cloud.soms.mapper.CmpaypicsMapper;
import com.dingreading.cloud.soms.service.CmpaypicsService;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class CmpaypicsServiceImpl extends BaseServiceImpl<CmpaypicsMapper, Cmpaypics, CmpaypicsTableDef> implements CmpaypicsService {

    @Override
    protected CmpaypicsTableDef getTable() {
        return CmpaypicsTableDef.cmpaypics;
    }

    @Override
    public int dropByOrderCode(String orderCode) {
        return getMapper().deleteByCondition(table.orderCode.eq(orderCode));
    }

    @Override
    public int dropByOrderCodeAndIdNot(String orderCode, List<Long> ids) {
        QueryCondition condition = table.orderCode.eq(orderCode);
        if (CollectionUtils.isNotEmpty(ids))
            condition.and(table.id.notIn(ids));
        return getMapper().deleteByCondition(condition);
    }

    @Override
    public List<Cmpaypics> getByOrderCode(String orderCode) {
        return getMapper().selectListByCondition(table.orderCode.eq(orderCode));
    }

    @Override
    public void unbindReceipt(String storeUid, String orderCode, String receiptUid) {
        QueryCondition condition = table.orderCode.eq(orderCode)
                .and(table.receiptUid.eq(receiptUid));
        getMapper().deleteByCondition(condition);
    }

    @Override
    public List<String> urlListByOrderCodeAndIdNot(String orderCode, List<Long> ids) {
        QueryCondition condition = table.orderCode.eq(orderCode);
        if (CollectionUtils.isNotEmpty(ids))
            condition.and(table.id.notIn(ids));
        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.imgUrl)
                .from(table)
                .where(condition);
        return getMapper().selectObjectListByQueryAs(wrapper, String.class);
    }

    @Override
    public List<String> urlListByOrderCode(String orderCode) {
        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.imgUrl)
                .from(table)
                .where(table.orderCode.eq(orderCode));
        return getMapper().selectObjectListByQueryAs(wrapper, String.class);
    }

}
